/**
 * Eclipse Outliner
 * Copyright (C) 2006 Jeff Mesnil
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.jmesnil.outliner.ui.actions;

import net.jmesnil.outliner.ui.editors.OutlinerEditor;
import net.jmesnil.outliner.ui.internal.OutlinerEditorInput;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;

public class OpenExternalFileAction extends Action implements IWorkbenchAction {

    public  static final String ID = "net.jmesnil.opml.ui.actions.OpenExternalFile";
    
    private IWorkbenchWindow window;

    private String filterPath;

    public OpenExternalFileAction(IWorkbenchWindow window) {
        this.window = window;
        setId(ID);
        setActionDefinitionId(ID);
        setAccelerator(SWT.MOD1 | 'O');

        filterPath = System.getProperty("user.home"); //$NON-NLS-1$
        setDescription("&Open File...");
        setText("Open...");
    }

    public void dispose() {
        window = null;
        filterPath = null;
    }

    public void selectionChanged(IAction action, ISelection selection) {
    }

    public void run(IAction action) {
        run();
    }

    public void run() {
        FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN
                | SWT.MULTI);
        dialog.setText("Open File");
        dialog.setFilterPath(filterPath);
        dialog.setFilterNames(new String[] {"*.opml"});
        dialog.open();
        String[] names = dialog.getFileNames();

        if (names != null) {
            filterPath = dialog.getFilterPath();

            for (int i = 0; i < names.length; i++) {
                IFileStore fileStore = EFS.getLocalFileSystem().getStore(
                        new Path(filterPath));
                fileStore = fileStore.getChild(names[i]);
                if (!fileStore.fetchInfo().isDirectory()
                        && fileStore.fetchInfo().exists()) {
                    IEditorInput input = new OutlinerEditorInput(fileStore);
                    IWorkbenchPage page = window.getActivePage();
                    try {
                        page.openEditor(input, OutlinerEditor.ID);
                    } catch (PartInitException e) {
                        MessageDialog.openError(window.getShell(),
                                "Open File Error", e.getMessage());
                    }
                }
            }
        }
    }
}
